<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    class Messages_c extends CI_Controller {
        function __construct() {
            parent::__construct();
            $this->load->model(array('messages_m', 'coordinator_m'));
        }
        
        function index() {
            $data['content'] = 'pages/messages/messages';
            $data['title'] = 'Messages';
            $data['current_page'] = 'inbox-page';
            $data['inbox'] = $this->messages_m->get_inbox($this->session->userdata('accid'));
            $data['sentbox'] = $this->messages_m->get_sentbox($this->session->userdata('accid'));
            //$data['drafts'] = $this->messages_m->get_draft($this->session->userdata('accid'));
            $data['recipients'] = $this->coordinator_m->get_recipients($this->session->userdata('accid'));
            $this->load->view('includes/template', $data);
        }
        
        function ajax_inbox(){
            echo json_encode(array('aaData'=>$this->messages_m->get_inbox($this->session->userdata('accid'))));
        }
        
        function ajax_sentbox() {
            echo json_encode(array('aaData'=>$this->messages_m->get_sentbox($this->session->userdata('accid'))));
        }
        
//        function ajax_draft() {
//            echo json_encode(array('aaData'=>$this->messages_m->get_draft($this->session->userdata('accid'))));
//        }
        
        function inbox_message($msg_id) {
            echo(json_encode($this->messages_m->inbox_msg($msg_id)));
        }
        
        function sentbox_message($msg_id) {
            echo(json_encode($this->messages_m->sentbox_msg($msg_id)));
        }
        
//        function draft_message($msg_id) {
//            echo(json_encode($this->messages_m->draft_msg($msg_id)));
//        }
        
        function recipients() {
            echo json_encode($this->coordinator_m->get_recipients($this->session->userdata('accid')));
        }
        
        function sender($acc_id) {
            echo json_encode($this->coordinator_m->get_sender_or_recipient($acc_id));
        }
        
        function send_message() {
            $file_element_name = $this->input->post('file_id');
            $status = '';
            $msg = '';
            if($this->input->post('userfile')!=''){
                $config['upload_path'] = './uploads/attachments';
                $config['allowed_types'] = '*';
                $this->load->library('upload', $config);
                
                if (!$this->upload->do_upload($file_element_name)){
                    $status = 'error';
                    $msg = $this->upload->display_errors('', '');
                }else{
                    $upload_data = $this->upload->data();
                    $msg_data = array(
                        'subject' => $this->input->post('subject'),
                        'msg_body' => $this->input->post('msg_body'),
                        'date' => $this->input->post('date_time'),
                        'sender_id' => $this->session->userdata('accid'),
                        'recipient_id' => $this->input->post('recipient'),
                        'read' => '0',
                        'attach_name' => $upload_data['file_name'],
                        'attach_path' => 'uploads/attachments/'.$upload_data['file_name'],
                        'action' => $this->input->post('action'),
                    );
                    $id = $this->messages_m->insert_msg($msg_data);
                    if($id){
                         $status = 'success';
                         if($this->input->post('action')==1){
                             $msg = 'Message successfully saved.';   
                         }else{
                             $msg = 'Message successfully sent.';   
                         }
                    }else{
                         unlink($upload_data['full_path']);
                         $status = "error";
                         $msg = 'Something went wrong when saving the file, please try again.';
                    }
                }
                @unlink($_FILES[$file_element_name]);
            }else{
                $msg_data = array(
                    'recipient_id' => $this->input->post('recipient'),
                    'sender_id' => $this->session->userdata('accid'),
                    'subject' => $this->input->post('subject'),
                    'msg_body' => $this->input->post('msg_body'),
                    'date' => $this->input->post('date_time'),
                    'read' => '0',
                    'attach_name' => '',
                    'attach_path' => '',
                    'action' => $this->input->post('action'),
                );
                $id = $this->messages_m->insert_msg($msg_data);
                if($id > 0){
                    $status = 'success';
                    if($this->input->post('action')==1){
                        $msg = 'Message successfully saved.';   
                    }else{
                        $msg = 'Message successfully sent.';   
                    }
                }else{
                    $status = 'error';
                    $msg = 'Oops! Failed to send messages.<br/> Message saved as draft.';
                }
            }
            echo json_encode(array('status' => $status, 'msg' => $msg));
        }
        
        function delete_inbox() {
            $msg_id = $this->input->post('msg_id');
            $msg = '';
            $this->messages_m->delete_msg_inbox($this->session->userdata('accid'), $msg_id);
            if(sizeof($msg_id)>1){
                $msg = 'Messages successfully deleted.';
            }else{
                $msg = 'Message successfully deleted.';
            }
            echo json_encode(array('msg'=>$msg));
        }
        
        function delete_sentbox() {
            $msg_id = $this->input->post('msg_id');
            $msg = '';
            $this->messages_m->delete_msg_sentbox($this->session->userdata('accid'), $msg_id);
            if(sizeof($msg_id)>1){
                $msg = 'Messages successfully deleted.';
            }else{
                $msg = 'Message successfully deleted.';
            }
            echo json_encode(array('msg'=>$msg));
        }
        
//        function change_status($id) {
//            $this->messages_m->change_status($this->session->userdata('accid'), $id);
//        }
    }
